package com.plan

import grails.converters.JSON

class ConstructionReportDetailController {

    
    def index = {
        redirect(action: "list", params: params)
    }

	def list = {
		def parent = ConstructionReport.get(params?.parentId?.toInteger())
		[constructionReportDetailInstanceList: parent?.details]
	}

    def create = {
        def constructionReportDetailInstance = new ConstructionReportDetail()
        def parent = ConstructionReport.get(params?.parentId)
        constructionReportDetailInstance.parent = parent
        constructionReportDetailInstance.properties = params
        return [parent:parent,constructionReportDetailInstance: constructionReportDetailInstance]
    }

    def save = {

        def constructionReportDetailInstance = new ConstructionReportDetail(params)
        if (constructionReportDetailInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), constructionReportDetailInstance.id])
            redirect(action: "edit", id: constructionReportDetailInstance.id)
        }
        else {
            render(view: "create", model: [constructionReportDetailInstance: constructionReportDetailInstance])
        }
    }

    def show = {
    		
        def constructionReportDetailInstance = ConstructionReportDetail.get(params.id)
        if (!constructionReportDetailInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), params.id])
            redirect(action: "list")
        }
        else {
            params.parentId = constructionReportDetailInstance.parent.Id
            [constructionReportDetailInstance: constructionReportDetailInstance]
        }
    }

    def edit = {
        def constructionReportDetailInstance = ConstructionReportDetail.get(params.id)
        if (!constructionReportDetailInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), params.id])
            redirect(action: "list")
        }
        else {
            params.parentId = constructionReportDetailInstance.parent.id
            return [constructionReportDetailInstance: constructionReportDetailInstance]
        }
    }

    def update = {
        def constructionReportDetailInstance = ConstructionReportDetail.get(params.id)
        if (constructionReportDetailInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (constructionReportDetailInstance.version > version) {
                    
                    constructionReportDetailInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail')] as Object[], "Another user has updated this ConstructionReportDetail while you were editing")
                    render(view: "edit", model: [constructionReportDetailInstance: constructionReportDetailInstance])
                    return
                }
            }
            constructionReportDetailInstance.properties = params
            if (!constructionReportDetailInstance.hasErrors() && constructionReportDetailInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), constructionReportDetailInstance.id])
                redirect(action: "edit", id: constructionReportDetailInstance.id)
            }
            else {
                render(view: "edit", model: [constructionReportDetailInstance: constructionReportDetailInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def constructionReportDetailInstance = ConstructionReportDetail.get(params.id)
        def parentId = constructionReportDetailInstance.parent.id
        if (constructionReportDetailInstance) {
            try {
                constructionReportDetailInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), params.id])
                redirect(controller:"constructionReport",action: "edit",id:parentId)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'constructionReportDetail.label', default: 'ConstructionReportDetail'), params.id])
            redirect(controller:"constructionReport",action: "edit",id:parentId)
        }
    }
}
